<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>新增课程</title>
    <script src="js/jquery-3.4.1.min.js"></script>
    <script>
        $(function () {
            $.getJSON("grades.cmd",function (grades) {
                var g=$("#gradeid");
                //alert(grades+","+typeof(grades));
                $.each(grades,function (i,d) {

                   g.append("<option value='"+d.id+"'>"+d.gname+"</option>");
                });
            });
            $(":text[name=subjectname]").blur(function () {
                if(this.value==""){
                    alert("课程名必须输入");
                    return;
                }
                var _name=this;
                $(_name).next().html('');
                $.post("checkSubjectName.cmd",{subjecname:this.value},function (rtn) {
                    alert(rtn);
                    if(rtn=="true"){
                        $(_name).next().html("此课程名已经被使用了");
                    }else
                        $(_name).next().html("此课程名可以使用");
                },"text").fail(function (e) {
                    alert("出现了错误，"+e.responseText);
                });
            });
        })
    </script>
    <script>
      /*  function checkSubject(t) {
            if(t.value=="")
            {
                alert("课程名必须输入");
                return;
            }
            var xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function (ev) {
                if(xhr.readyState==4&&xhr.status==200){
                    //console.log(xhr.getAllResponseHeaders());
                    let rtn=xhr.responseText;//对应服务器端的print
                    if(rtn=="true"){
                        alert("此课程名已经被使用了");
                    }else
                        alert("此课程名可以使用");

                }
            }
            xhr.open("get","checkSubjectName.cmd?subjecname="+t.value,true);
            xhr.send(null);
        }*/
    </script>
</head>
<body>
<%@include file="/WEB-INF/jsp/checkLogin.jsp"%>

 <form method="post" action="addSubject">
     <fieldset>
         <label>课程信息</label>
         <p>
             <input type="text" required name="subjectname" placeholder="课程名" <%--onblur="checkSubject(this)"--%>/><span style="color:red"></span>
             <input type="number" min="0" required name="classhour" placeholder="学时" />
             <select name="gradeid" id="gradeid">
               <%-- <c:forEach items="${grades}" var="g">
                 <option value="${g.id}">${g.gname}</option>
                </c:forEach>--%>
             </select>
         </p>
         <p>
             <input type="submit" value="保存"/>
         </p>
     </fieldset>
 </form>
</body>
</html>
